package cn.edu.hbvtc.qms.persist.hibernate;

import java.util.List;

import cn.edu.hbvtc.qms.domain.Department;
import cn.edu.hbvtc.qms.domain.EvalPeriod;
import cn.edu.hbvtc.qms.domain.Term;
import cn.edu.hbvtc.qms.persist.IEvalPeriodDao;

public class HibernateEvalPeriodDao extends HibernateAbstractDao<EvalPeriod, Integer>
		implements IEvalPeriodDao {

	private static final long serialVersionUID = 1L;

	public HibernateEvalPeriodDao() {
		super(EvalPeriod.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public EvalPeriod findEvalPeriodByUserId(int userId) {
		List<EvalPeriod> epList = this.getHibernateTemplate().find("from EvalPeriod e where e.deptAdmin.id = ? and e.enabled = 1", userId);
		EvalPeriod ep = null;
		if (epList != null && epList.size() > 0) {
			ep = epList.get(0);
		}
		return ep;
	}

	@SuppressWarnings("unchecked")
	@Override
	public EvalPeriod findEvalPeriodByDepartment(Department dept, Term term) {
		List<EvalPeriod> epList = this.getHibernateTemplate().find("from EvalPeriod e where e.dept.id = ? and e.term.id = ? and e.enabled = 1", new Object[]{dept.getId(), term.getId()});
		EvalPeriod ep = null;
		if (epList != null && epList.size() > 0) {
			ep = epList.get(0);
		}
		return ep;
	}
	
	//获得当前学期的最新评价时间段对象
	@SuppressWarnings("unchecked")
	@Override
	public EvalPeriod findLateEvalPeriodByDepartmentInThisTerm(Department dept, Term term) {
		List<EvalPeriod> epList = this.getHibernateTemplate().find("from EvalPeriod e where e.dept.id = ? and e.term.id = ? and e.enabled = 1 order by e.id desc", new Object[]{dept.getId(), term.getId()});
		EvalPeriod ep = null;
		if (epList != null && epList.size() > 0) {
			ep = epList.get(0);
		}
		return ep;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EvalPeriod> findEvalPeriodsInDeptAndTerm(Department dept, Term term) {
		List<EvalPeriod> epList = this.getHibernateTemplate().find("from EvalPeriod e where e.dept.id = ? and e.term.id = ? order by e.id desc", new Object[]{dept.getId(), term.getId()});
		return epList;
	}	

}
